package com.pignest.hotel.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;

import com.pignest.hotel.beans.Duty;
import com.pignest.hotel.beans.DutyLog;
import com.pignest.hotel.beans.Log;
import com.pignest.hotel.servlet.QueryServlet;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.DateUtil;

public class AdminService extends BaseService implements InitializingBean {
	
	public String getLogUrl(HttpServletRequest request) throws UnsupportedEncodingException {
		String userName = request.getParameter("userName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String ip = request.getParameter("ip");
		String keyword = request.getParameter("keyword");
//		int pageIndex = Utils.getInteger(request.getParameter("pi"), 0);
		StringBuilder url = new StringBuilder(request.getContextPath());
		url.append("/").append(QueryServlet.FILTER).append("/log");
		String enc = "UTF-8";
		char c = '?';
		if (StringUtils.isNotEmpty(userName)) {
			url.append(c);
			c = '&';
			url.append("userName=").append(URLEncoder.encode(userName, enc));
		}
		if (StringUtils.isNotEmpty(startTime)) {
			url.append(c);
			c = '&';
			url.append("startTime=").append(startTime);
		}
		if (StringUtils.isNotEmpty(endTime)) {
			url.append(c);
			c = '&';
			url.append("endTime=").append(endTime);
		}
		if (StringUtils.isNotEmpty(ip)) {
			url.append(c);
			c = '&';
			url.append("ip=").append(ip);
		}
		if (StringUtils.isNotEmpty(keyword)) {
			url.append(c);
			c = '&';
			url.append("keyword=").append(URLEncoder.encode(keyword, enc));
		}
		return url.toString();
	}
	
	public String getDutyUrl(HttpServletRequest request) throws UnsupportedEncodingException {
		String userName = request.getParameter("userName");
		String status = request.getParameter("status");
		StringBuilder url = new StringBuilder(request.getContextPath());
		url.append("/").append(QueryServlet.FILTER).append("/duty");
		String enc = "UTF-8";
		char c = '?';
		if (StringUtils.isNotEmpty(userName)) {
			url.append(c);
			c = '&';
			url.append("userName=").append(URLEncoder.encode(userName, enc));
		}
		if (StringUtils.isNotEmpty(status)) {
			url.append(c);
			c = '&';
			url.append("status=").append(status);
		}
		return url.toString();
	}
	
	public String getDutyLogUrl(HttpServletRequest request) {
		String dutyId = request.getParameter("duty");
		StringBuilder url = new StringBuilder(request.getContextPath());
		url.append("/").append(QueryServlet.FILTER).append("/dutylog?duty=").append(dutyId);
		return url.toString();
	}

	public int countLogs(String userName, Date startTime, Date endTime, String ip, String keyword) {
		StringBuilder s = new StringBuilder();
		List<Object> values = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(userName)) {
			s.append("userName=?");
			values.add(userName);
		}
		if (StringUtils.isNotEmpty(ip)) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("ip=?");
			values.add(ip);
		}
		if (startTime != null) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("time>=?");
			values.add(startTime);
		}
		if (endTime != null) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("time<=?");
			values.add(new Date(endTime.getTime() + DateUtil.ONE_DAY));
		}
		if (StringUtils.isNotEmpty(keyword)) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("(");
			String key = "%" + keyword + "%";
			s.append(" action like ? or category like ? or description like ? or parameter like ? ");
			values.add(key);
			values.add(key);
			values.add(key);
			values.add(key);
			s.append(")");
		}
		return count(Log.class, s.toString(), values.toArray(new Object[values.size()]), null);
	}
	
	public List<Log> listLogs(String userName, Date startTime, Date endTime, String ip, String keyword,
			String orderBy, int pageSize, int pageIndex) {
		StringBuilder s = new StringBuilder();
		List<Object> values = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(userName)) {
			s.append("userName=?");
			values.add(userName);
		}
		if (StringUtils.isNotEmpty(ip)) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("ip=?");
			values.add(ip);
		}
		if (startTime != null) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("time>=?");
			values.add(startTime);
		}
		if (endTime != null) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("time<=?");
			values.add(endTime);
		}
		if (StringUtils.isNotEmpty(keyword)) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("(");
			String key = "%" + keyword + "%";
			s.append(" action like ? or category like ? or description like ? or parameter like ? ");
			values.add(key);
			values.add(key);
			values.add(key);
			values.add(key);
			s.append(")");
		}
		if (StringUtils.isNotEmpty(orderBy)) {
			if (s.length() > 0)
				s.append(" ");
			s.append("order by ").append(orderBy);
		}
		return query(Log.class, s.toString(), pageSize, pageIndex, values.toArray(new Object[values.size()]), null);
	}
	
	public int countDuties(String userName, int status) {
		StringBuilder s = new StringBuilder();
		List<Object> values = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(userName)) {
			s.append("user.name=?");
			values.add(userName);
		}
		if (status >= 0) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("status=?");
			values.add(Integer.valueOf(status));
		}
		return count(Duty.class, s.toString(), values.toArray(new Object[values.size()]), null);
	}

	public List<Duty> listDuties(String userName, int status, String orderBy, int pageSize, int pageIndex) {
		StringBuilder s = new StringBuilder();
		List<Object> values = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(userName)) {
			s.append("user.name=?");
			values.add(userName);
		}
		if (status >= 0) {
			if (s.length() > 0)
				s.append(" and ");
			s.append("status=?");
			values.add(Integer.valueOf(status));
		}
		if (StringUtils.isNotEmpty(orderBy)) {
			if (s.length() > 0)
				s.append(" ");
			s.append("order by ").append(orderBy);
		}
		return query(Duty.class, s.toString(), pageSize, pageIndex, values.toArray(new Object[values.size()]), null);
	}

	public int countDutyLogs(String dutyId) {
		return count(DutyLog.class, "duty.id=?", new Object[] {dutyId}, null);
	}
	
	public List<DutyLog> listDutyLogs(String dutyId, int pageSize, int pageIndex) {
		return query(DutyLog.class, "duty.id=?", pageSize, pageIndex, new Object[] {dutyId}, null);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		L.setService(this);
	}
}
